package com.mlethe.library.imagepreview;

import androidx.annotation.Nullable;

import android.app.Activity;
import android.content.Context;
import android.content.Intent;
import android.os.Bundle;
import android.os.Parcelable;

import androidx.annotation.NonNull;
import androidx.fragment.app.Fragment;

import com.mlethe.library.imagepreview.enitity.IThumbViewInfo;
import com.mlethe.library.imagepreview.loader.VideoClickListener;
import com.mlethe.library.imagepreview.view.BasePhotoFragment;

import java.io.Serializable;
import java.util.ArrayList;
import java.util.List;

/**
 * Created by Mlethe on 2017/9/12.
 */
public final class GPreviewBuilder {
    private Context mContext;
    private Intent intent;
    private Class<?> className;
    private VideoClickListener videoClickListener;

    private GPreviewBuilder(@NonNull Context context) {
        mContext = context;
        intent = new Intent();
    }

    /***
     * 设置开始启动预览
     * @param context  启动
     *@return GPreviewBuilder
     * **/
    public static GPreviewBuilder from(@NonNull Context context) {
        return new GPreviewBuilder(context);
    }

    /***
     * 设置开始启动预览（推荐）
     * @param activity  启动
     *@return GPreviewBuilder
     * **/
    public static GPreviewBuilder from(@NonNull Activity activity) {
        return new GPreviewBuilder(activity);
    }

    /***
     * 设置开始启动预览（推荐）
     * @param fragment  启动
     *@return GPreviewBuilder
     * **/
    public static GPreviewBuilder from(@NonNull Fragment fragment) {
        return new GPreviewBuilder(fragment.requireActivity());
    }

    /****
     *自定义预览activity 类名
     * @param className   继承GPreviewActivity
     *@return GPreviewBuilder
     * **/
    public GPreviewBuilder to(@NonNull Class<?> className) {
        this.className = className;
        intent.setClass(mContext, className);
        return this;
    }

    /****
     *自定义预览activity 类名  方便自定义数据。
     * @param className 继承GPreviewActivity
     * @param bundle 需携带的参数
     *@return GPreviewBuilder
     * **/
    public GPreviewBuilder to(@NonNull Class<?> className, @NonNull Bundle bundle) {
        this.className = className;
        intent.setClass(mContext, className);
        intent.putExtras(bundle);
        return this;
    }

    /***
     * 设置数据源
     * @param imgUrls 数据
     *@param   <T>    你的实体类类型
     * @return GPreviewBuilder
     * **/
    public <T extends IThumbViewInfo> GPreviewBuilder setData(@NonNull List<T> imgUrls) {
        intent.putParcelableArrayListExtra("imagePaths", new ArrayList<Parcelable>(imgUrls));
        return this;
    }

    /***
     * 设置单个数据源
     * @param imgUrl 数据
     *@param   <E>    你的实体类类型
     * @return GPreviewBuilder
     * **/
    public <E extends IThumbViewInfo> GPreviewBuilder setSingleData(@NonNull E imgUrl) {
        ArrayList<E> arrayList = new ArrayList<>();
        arrayList.add(imgUrl);
        intent.putParcelableArrayListExtra("imagePaths", arrayList);
        return this;
    }

    /**
     * 是否自定义数据
     *
     * @param isCustom true 自定义数据，默认 false
     * @return
     */
    public GPreviewBuilder isCustomData(boolean isCustom) {
        intent.putExtra("isCustomData", isCustom);
        return this;
    }

    /***
     * 设置数据源
     * @param className 你的Fragment类
     * @return GPreviewBuilder
     * **/
    public GPreviewBuilder setUserFragment(@NonNull Class<? extends BasePhotoFragment> className) {
        intent.putExtra("className", className);
        return this;
    }

    /***
     * 设置默认索引
     * @param currentIndex 数据
     * @return GPreviewBuilder
     * **/
    public GPreviewBuilder setCurrentIndex(int currentIndex) {
        intent.putExtra("position", currentIndex);
        return this;
    }

    /***
     * 设置指示器类型
     * @param indicatorType 枚举
     * @return GPreviewBuilder
     * **/
    public GPreviewBuilder setType(@NonNull IndicatorType indicatorType) {
        intent.putExtra("type", indicatorType);
        return this;
    }

    /***
     * 设置图片禁用拖拽返回
     * @param isDrag  true  可以 false 默认 true
     * @return GPreviewBuilder
     * **/
    public GPreviewBuilder isDisableDrag(boolean isDrag) {
        intent.putExtra("isDrag", isDrag);
        return this;
    }

    /***
     * 设置图片禁用拖拽返回
     * @param isDrag  true  可以 false 默认 true
     * @param sensitivity   sensitivity MAX_TRANS_SCALE 的值来控制灵敏度。
     * @return GPreviewBuilder
     * **/
    public GPreviewBuilder isDisableDrag(boolean isDrag, float sensitivity) {
        intent.putExtra("isDrag", isDrag);
        intent.putExtra("sensitivity", sensitivity);
        return this;
    }

    /***
     * 是否设置为一张图片时 显示指示器  默认显示
     * @param isShow   true  显示 false 不显示
     * @return GPreviewBuilder
     * **/
    public GPreviewBuilder setSingleShowType(boolean isShow) {
        intent.putExtra("isShow", isShow);
        return this;
    }

    /***
     * 设置超出内容点击退出（黑色区域）
     * @param isSingleFling  true  可以 false
     * @return GPreviewBuilder
     * **/
    public GPreviewBuilder setSingleFling(boolean isSingleFling) {
        intent.putExtra("isSingleFling", isSingleFling);
        return this;
    }

    /***
     *  设置动画的时长
     * @param setDuration  单位毫秒
     * @return GPreviewBuilder
     * **/
    public GPreviewBuilder setDuration(int setDuration) {
        intent.putExtra("duration", setDuration);
        return this;
    }

    /***
     *  设置是否全屏
     * @param isFullscreen  true 全屏，false 不全屏
     * @return GPreviewBuilder
     * **/
    public GPreviewBuilder setFullscreen(boolean isFullscreen) {
        intent.putExtra("isFullscreen", isFullscreen);
        return this;
    }

    /***
     *  禁止图片没有放大或者的缩小状态触退出
     * @param isScale    true false
     * @return GPreviewBuilder
     * **/
    public GPreviewBuilder isDisableScale(boolean isScale) {
        intent.putExtra("isScale", isScale);
        return this;
    }

    /***
     *  设置只有图片没有放大或者的缩小状态触退出
     * @param scaleCount    默认 3，值：3，2（类似微信），其他（没有双击缩放）
     * @return GPreviewBuilder
     * **/
    public GPreviewBuilder setScaleCount(int scaleCount) {
        intent.putExtra("scaleCount", scaleCount);
        return this;
    }

    /***
     *  设置是视频点击播放回调
     * @return GPreviewBuilder
     * **/
    public GPreviewBuilder setOnVideoPlayerListener(VideoClickListener listener) {
        this.videoClickListener = listener;
        return this;
    }

    /**
     * 添加拓展数据
     *
     * @param name  key
     * @param value 值
     * @return GPreviewBuilder
     */
    public GPreviewBuilder putExtra(String name, boolean value) {
        intent.putExtra(name, value);
        return this;
    }

    /**
     * 添加拓展数据
     *
     * @param name  key
     * @param value 值
     * @return GPreviewBuilder
     */
    public GPreviewBuilder putExtra(String name, byte value) {
        intent.putExtra(name, value);
        return this;
    }

    /**
     * 添加拓展数据
     *
     * @param name  key
     * @param value 值
     * @return GPreviewBuilder
     */
    public GPreviewBuilder putExtra(String name, char value) {
        intent.putExtra(name, value);
        return this;
    }

    /**
     * 添加拓展数据
     *
     * @param name  key
     * @param value 值
     * @return GPreviewBuilder
     */
    public GPreviewBuilder putExtra(String name, short value) {
        intent.putExtra(name, value);
        return this;
    }

    /**
     * 添加拓展数据
     *
     * @param name  key
     * @param value 值
     * @return GPreviewBuilder
     */
    public GPreviewBuilder putExtra(String name, int value) {
        intent.putExtra(name, value);
        return this;
    }

    /**
     * 添加拓展数据
     *
     * @param name  key
     * @param value 值
     * @return GPreviewBuilder
     */
    public GPreviewBuilder putExtra(String name, long value) {
        intent.putExtra(name, value);
        return this;
    }

    /**
     * 添加拓展数据
     *
     * @param name  key
     * @param value 值
     * @return GPreviewBuilder
     */
    public GPreviewBuilder putExtra(String name, float value) {
        intent.putExtra(name, value);
        return this;
    }

    /**
     * 添加拓展数据
     *
     * @param name  key
     * @param value 值
     * @return GPreviewBuilder
     */
    public GPreviewBuilder putExtra(String name, double value) {
        intent.putExtra(name, value);
        return this;
    }

    /**
     * 添加拓展数据
     *
     * @param name  key
     * @param value 值
     * @return GPreviewBuilder
     */
    public GPreviewBuilder putExtra(String name, @Nullable String value) {
        intent.putExtra(name, value);
        return this;
    }

    /**
     * 添加拓展数据
     *
     * @param name  key
     * @param value 值
     * @return GPreviewBuilder
     */
    public GPreviewBuilder putExtra(String name, @Nullable CharSequence value) {
        intent.putExtra(name, value);
        return this;
    }

    /**
     * 添加拓展数据
     *
     * @param name  key
     * @param value 值
     * @return GPreviewBuilder
     */
    public GPreviewBuilder putExtra(String name, @Nullable Parcelable value) {
        intent.putExtra(name, value);
        return this;
    }

    /**
     * 添加拓展数据
     *
     * @param name  key
     * @param value 值
     * @return GPreviewBuilder
     */
    public GPreviewBuilder putExtra(String name, @Nullable Parcelable[] value) {
        intent.putExtra(name, value);
        return this;
    }

    /**
     * 添加拓展数据
     *
     * @param name  key
     * @param value 值
     * @return GPreviewBuilder
     */
    public GPreviewBuilder putParcelableArrayListExtra(String name, @Nullable ArrayList<? extends Parcelable> value) {
        intent.putExtra(name, value);
        return this;
    }

    /**
     * 添加拓展数据
     *
     * @param name  key
     * @param value 值
     * @return GPreviewBuilder
     */
    public GPreviewBuilder putIntegerArrayListExtra(String name, @Nullable ArrayList<Integer> value) {
        intent.putExtra(name, value);
        return this;
    }

    /**
     * 添加拓展数据
     *
     * @param name  key
     * @param value 值
     * @return GPreviewBuilder
     */
    public GPreviewBuilder putStringArrayListExtra(String name, @Nullable ArrayList<String> value) {
        intent.putExtra(name, value);
        return this;
    }

    /**
     * 添加拓展数据
     *
     * @param name  key
     * @param value 值
     * @return GPreviewBuilder
     */
    public GPreviewBuilder putCharSequenceArrayListExtra(String name, @Nullable ArrayList<CharSequence> value) {
        intent.putExtra(name, value);
        return this;
    }

    /**
     * 添加拓展数据
     *
     * @param name  key
     * @param value 值
     * @return GPreviewBuilder
     */
    public GPreviewBuilder putExtra(String name, @Nullable Serializable value) {
        intent.putExtra(name, value);
        return this;
    }

    /**
     * 添加拓展数据
     *
     * @param name  key
     * @param value 值
     * @return GPreviewBuilder
     */
    public GPreviewBuilder putExtra(String name, @Nullable boolean[] value) {
        intent.putExtra(name, value);
        return this;
    }

    /**
     * 添加拓展数据
     *
     * @param name  key
     * @param value 值
     * @return GPreviewBuilder
     */
    public GPreviewBuilder putExtra(String name, @Nullable byte[] value) {
        intent.putExtra(name, value);
        return this;
    }

    /**
     * 添加拓展数据
     *
     * @param name  key
     * @param value 值
     * @return GPreviewBuilder
     */
    public GPreviewBuilder putExtra(String name, @Nullable short[] value) {
        intent.putExtra(name, value);
        return this;
    }

    /**
     * 添加拓展数据
     *
     * @param name  key
     * @param value 值
     * @return GPreviewBuilder
     */
    public GPreviewBuilder putExtra(String name, @Nullable char[] value) {
        intent.putExtra(name, value);
        return this;
    }

    /**
     * 添加拓展数据
     *
     * @param name  key
     * @param value 值
     * @return GPreviewBuilder
     */
    public GPreviewBuilder putExtra(String name, @Nullable int[] value) {
        intent.putExtra(name, value);
        return this;
    }

    /**
     * 添加拓展数据
     *
     * @param name  key
     * @param value 值
     * @return GPreviewBuilder
     */
    public GPreviewBuilder putExtra(String name, @Nullable long[] value) {
        intent.putExtra(name, value);
        return this;
    }

    /**
     * 添加拓展数据
     *
     * @param name  key
     * @param value 值
     * @return GPreviewBuilder
     */
    public GPreviewBuilder putExtra(String name, @Nullable float[] value) {
        intent.putExtra(name, value);
        return this;
    }

    /**
     * 添加拓展数据
     *
     * @param name  key
     * @param value 值
     * @return GPreviewBuilder
     */
    public GPreviewBuilder putExtra(String name, @Nullable double[] value) {
        intent.putExtra(name, value);
        return this;
    }

    /**
     * 添加拓展数据
     *
     * @param name  key
     * @param value 值
     * @return GPreviewBuilder
     */
    public GPreviewBuilder putExtra(String name, @Nullable String[] value) {
        intent.putExtra(name, value);
        return this;
    }

    /**
     * 添加拓展数据
     *
     * @param name  key
     * @param value 值
     * @return GPreviewBuilder
     */
    public GPreviewBuilder putExtra(String name, @Nullable CharSequence[] value) {
        intent.putExtra(name, value);
        return this;
    }

    /**
     * 添加拓展数据
     *
     * @param name  key
     * @param value 值
     * @return GPreviewBuilder
     */
    public GPreviewBuilder putExtra(String name, @Nullable Bundle value) {
        intent.putExtra(name, value);
        return this;
    }

    /***
     * 启动
     * **/
    public void start() {
        if (className == null) {
            intent.setClass(mContext, GPreviewActivity.class);
        } else {
            intent.setClass(mContext, className);
        }
        if (!(mContext instanceof Activity)) {
            intent.addFlags(Intent.FLAG_ACTIVITY_NEW_TASK);
        }
        BasePhotoFragment.listener = videoClickListener;
        mContext.startActivity(intent);
        if (mContext instanceof Activity) {
            ((Activity) mContext).overridePendingTransition(0, 0);
        }
        videoClickListener = null;
        intent = null;
        mContext = null;
    }

    /***
     * 指示器类型
     * ***/
    public enum IndicatorType {
        Dot, Number
    }
}
